<template>
  <el-container class="home">
    <!--    头部区域-->
    <el-header>
      <div>
        <img src="../../assets/heima.png" alt="黑马图片">
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!--      侧边栏区域-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!--        折叠开关-->
        <div class="toggle-button" @click="clickCollapse">|||</div>
        <!--        菜单-->
        <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            active-text-color="#409eff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
        >
          <MenuItem v-for="item in menulist" :item="item" :key="item.id"></MenuItem>

        </el-menu>
      </el-aside>

      <!--      主体区域-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import MenuItem from './MenuItem.vue'
import {GetMenuList, LogOut} from "@/api";
import {computed, watch, ref} from "vue";
import {useRouter, useRoute} from "vue-router";
import {useStore} from "@/store";
// 头部区域
const router = useRouter()
const route = useRoute()
const store = useStore()

// 设置defalut-active
// const activePath = computed(()=>{
//   return store.getters.getActivaPath
// })
// watch(()=>route.path,()=>{
//   store.commit('setActivePath', route.path)
// })
const activePath = ref(route.path)
watch(()=>route.path, ()=>{
  activePath.value = route.path
})

// 退出
const logout = async ()=>{
  LogOut()
  await router.push('/login')
}

// 侧边栏区域
// 设置菜单折叠展开
const isCollapse = computed(()=>{
  return store.getters.getCollapse
})
const clickCollapse = ()=>{
  store.commit('setCollapse', !isCollapse.value)
}
// 获取菜单列表
const menulist = computed(()=>{
  return store.getters.getMenuList
})
const getMenuList = async ()=>{
  const {data: result} = await GetMenuList()
  store.commit('setMenuList', result.data)
}
getMenuList()
</script>

<style lang="less" scoped>
.home{
  height: 100%;
}
.el-header{
  background-color: #393632;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ccc;
  font-size: 20px;
  div{
    display: flex;
    align-items: center;
    img{
      margin-right: 15px;
    }
  }
}
.el-aside{
  //background-color: #0086b3;
  .toggle-button{
    // 添加背景颜色
    background-color: #4A5064;
    // 设置文本大小
    font-size:10px;
    // 设置文本行高
    line-height:24px;
    // 设置文本颜色
    color:#fff;
    // 设置文本居中
    text-align: center;
    // 设置文本间距
    letter-spacing: 0.2em;
    // 设置鼠标悬浮变小手效果
    cursor:pointer;
  }
}
.el-main{
  //background-color: #cccccc;
}
</style>
